package com.lb.statck.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.statck.common.result.Result;
import com.lb.statck.mapper.order.OrderMapper;
import com.lb.statck.pojo.dto.OrderDTO;
import com.lb.statck.pojo.dto.OrderQueryDto;
import com.lb.statck.pojo.vo.OrderVo;
import com.lb.statck.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
 @RestController
@RequestMapping("/api/order")
public class ordercontroller {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;
    @GetMapping("/getOrder")
    public Result getOrder( @RequestParam(value = "userId") int id) {
        List<OrderVo> listOrder = orderService.getListOrder(id);
        Result result = new Result();
        result.setData(listOrder);
        result.setCode(200);
        result.setMsg("success");
        return result;
    }
    @PostMapping("/getOrder1")
    public Result getOrder1(@RequestBody OrderQueryDto orderQueryDto) {
        PageHelper.startPage(orderQueryDto.getPageNum(), orderQueryDto.getPageSize());
        List<OrderVo> orders = orderService.getOrders(orderQueryDto);
        PageInfo<OrderVo> pageInfo = new PageInfo<>(orders);
        Result result = new Result();
        result.setData(pageInfo);
        result.setCode(200);
        result.setMsg("success");
        return result;
    }
    @PostMapping("/insertOrder")
    public Result insertOrder(  @RequestBody OrderDTO orderVo) {
        //List<OrderVo> listOrder = orderService.getListOrder(id);
        Result result = new Result();
        String s = orderService.insertOrder(orderVo);
        //result.setData(listOrder);
        result.setCode(200);
        result.setData(s);
        result.setMsg("success");
        return result;
    }
    @GetMapping("/getByOrder")
   public Result getByOrder( @RequestParam(value = "payOrder") String orderId) {
        Result result = new Result();
        OrderVo order = orderService.getOrder(orderId);
        if (order==null)
        {
            result.setCode(200);
        }
        else {
            result.setCode(300);
        }
     //   Result result = new Result();
      // result.setCode(200);
       result.setMsg("success");
       result.setData(order);
       return result;

    }
    @PostMapping("/updateOrder")
    public Result updateOrder( @RequestBody OrderDTO orderVo) {
        Result result = new Result();
        //UpdateWrapper<OrderVo> updateWrapper = new UpdateWrapper<>();
        //updateWrapper.eq("order_id", orderVo.getOrderId());
        orderService.updateOrder(orderVo);
        result.setCode(200);
        result.setMsg("success");
        return result;
    }
    @PostMapping("getOrder")
    public Result getOrder(@RequestBody OrderDTO orderVo)
    {      List<Map<String, Object>>   orderVo1=   orderService.getOrder1(orderVo);
        Result result = new Result();
        result.setData(orderVo1);
        result.setCode(200);
        result.setMsg("success");
        return result;
    }
}
